from __future__ import annotations

import secrets
from typing import Any, Dict, List, Optional, Union

from pydantic import AnyHttpUrl, BaseSettings, EmailStr, HttpUrl, PostgresDsn, validator

import os
from pathlib import Path
class Settings(BaseSettings):
    API_V1_STR: str = "/api/v1"
    SECRET_KEY: str = secrets.token_urlsafe(32)

    DB_HOST: str
    DB_USER: str
    DB_PASSWORD: str
    DB_NAME: str
    DB_PORT: int
    SQLALCHEMY_DATABASE_URI:str = f"mysql+mysqldb://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"

    BACKEND_CORS_ORIGINS: List[Any] = []
    #阿里云OSS配置
    OSS_ENDPOINT:str=""
    OSS_AccessKeyID:str=""
    OSS_AccessKeySecret:str=""
    OSS_BUCKET:str=""


    class Config:
        case_sensitive = True
        env_file = os.path.expanduser((Path(__file__).parent.parent.parent/".env").as_posix())

settings = Settings()

from fastapi.openapi.docs import get_swagger_ui_html
def swagger_monkey_patch(*args, **kwargs):
    """
    fastapi的swagger ui默认使用国外cdn, 所以导致文档打不开, 需要对相应方法做替换
    在应用生效前, 对swagger ui html做替换
    :param args:
    :param kwargs:
    :return:
    """
    return get_swagger_ui_html(
        *args, **kwargs,
        swagger_js_url='https://cdn.staticfile.org/swagger-ui/5.9.0/swagger-ui-bundle.min.js',  # 改用国内cdn
        swagger_css_url='https://cdn.staticfile.org/swagger-ui/5.9.0/swagger-ui.min.css'
    )